package com.houserent.action;

import action.pojo.Auctionuser;

import my.framework.dao.CommonDao;
import my.framework.dao.DatabaseUtil;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import java.time.temporal.ChronoUnit;
import java.util.List;

public class UserAction {
    /**
     * 登录方法
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */

    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //接收用户输入的账号和密码
        Auctionuser user = findById(request, response);

        //判断接收的值是否为空
        if(user!=null){
            //把用户信息存储在Session中
            request.getSession().setAttribute("user",user);
            //登录成功
            return "/list.jsp";
        }else {
            //登录失败则回到登录页面
            return "/login.jsp";  //  转发
        }
    }

    /**
     * 注册方法
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */

    public String register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Connection connection = DatabaseUtil.getConnection();
        //sql
        String sql="insert into auctionuser( userName , userPassword , userCardNo , userTel , userAddress ,userPostNumber)" +
                "VALUES(?,?,?,?,?,?)";

        //获取用户输入的信息
        String username=request.getParameter("username");
        String userpassword=request.getParameter("userpassword");
        String usercardno=request.getParameter("usercardno");
        String usertel=request.getParameter("usertel");
        String useraddress=request.getParameter("useraddress");
        String userpostnumber=request.getParameter("userpostnumber");

        //判断用户是否存在
        Auctionuser existingUser=findUserByUsername(username);

        if(existingUser != null){
            //用户存在,返回错误信息重定向到注册页面并显示错误
            request.setAttribute("errorMessage", "用户名已存在，请重新输入！");
            return "forward:/register.jsp"; // 使用转发回到注册页面并显示错误
        }

        Object[] params={username,userpassword,usercardno,usertel,useraddress,userpostnumber};

        CommonDao commonDao=new CommonDao();
        //调用工具类方法
        int result=commonDao.executeUpdate(sql,params);
        connection.commit();
        if(result>0){
            return "redirect:/login.jsp"; // 使用重定向到登录页面
        }else {
            request.setAttribute("errorMessage", "注册失败，请稍后再试！");
            return "forward:/register.jsp"; // 使用转发回到注册页面并显示错误
        }
    }

    /**
     * 验证登录
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     * @throws IOException
     * @throws SQLException
     */
    public Auctionuser findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException, SQLException {

        //获取用户输入的账号和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //sql
        String sql="select * from auctionuser where userName =? and userPassword =? ";

        //用来存储账号与密码
        Object[] o={username,password};
        CommonDao commonDao=new CommonDao();
        //调用工具类的方法
        List<Auctionuser> auctionusers = commonDao.executeQuery(Auctionuser.class, sql, o);

        //判断用户是否存在
        if(auctionusers.size()>0){
            //获取查询到的所有数据
            return auctionusers.get(0);
        }
        return null;
    }

    /**
     * 通过用户名判断用户是否存在
     * @param username
     * @return
     */
    public Auctionuser findUserByUsername(String username) throws SQLException {
        //sql语句
        String sql="select * from auctionuser where userName = ?";

        //存储用户输入的用户名
        Object[] params={username};

        CommonDao commonDao=new CommonDao();
        //调用工具类方法
        List<Auctionuser> auctionusers=commonDao.executeQuery(Auctionuser.class,sql,params);

        //判断用户是否存在
        if(auctionusers.size()>0){
            return auctionusers.get(0);
        }
        return null;
    }
}
